<?php
//获取分类和标签页
function cdn_get_term_urls($post_ID, $taxonomy='category') {
    $urls = array(
        'link' => [],
        'path' => []
    );

    $terms = get_the_terms($post_ID, $taxonomy);
    if($terms) {
        foreach($terms as $term) {
            $link = get_term_link($term, $taxonomy);
            $urls['link'][] = $link;
            if(get_option('permalink_structure')) {
                $urls['path'][] = $link . '/page/';
            }
        }
    }
    
    if($taxonomy == 'category') {
        $tags = cdn_get_term_urls($post_ID, 'post_tag');
        if(!empty($tags['link'])) {
            $urls['link'] = wp_parse_args($tags['link'], $urls['link']);
        }
        if(!empty($tags['path'])) {
            $urls['path'] = wp_parse_args($tags['path'], $urls['path']);
        }
    }
    return $urls;
}

//初始化CDN类
function cdn_init_class_cdn($type) {
    $tencentCDN = new tencentCDN;
    if(!$tencentCDN->options) {
        return false;
    }

    $flush_cache = $tencentCDN->options['flush_cache'];
    $flush_cache = explode(',', $flush_cache);
    if(!in_array($type, $flush_cache)) {
        return false;
    }

    return $tencentCDN;
}

//获取缓存规则
function cdn_rulecache() {
    $tencentCDN = new tencentCDN;
    if($tencentCDN->options) {
        $domains_config = $tencentCDN->domains_config();
        return isset($domains_config['Cache']['RuleCache']) ? $domains_config['Cache']['RuleCache'] : '';
    }
}

//更新文章时刷新缓存
function cdn_post_updated($post_ID) {
	$tencentCDN = cdn_init_class_cdn('uppost');
    if($tencentCDN === false) {
        return;
    }

	$post_url = get_permalink($post_ID);
    $tencentCDN->flush_urls([$post_url]);
}
add_action('post_updated', 'cdn_post_updated', 10, 1);

//文章状态变更时刷新缓存
function cdn_post_change($post_ID) {
	$tencentCDN = cdn_init_class_cdn('changepost');
    if($tencentCDN === false) {
        return;
    }

	$post_url = get_permalink($post_ID);
    $post = get_post($post_ID);
    if('post' == $post->post_type) {
        $home_url = home_url();
        $urls = cdn_get_term_urls($post_ID);
        array_push($urls['link'], $home_url, $post_url);
        
        if(get_option('permalink_structure')) {
            array_push($urls['path'], $home_url . '/page/');
        }

        $tencentCDN->flush_urls($urls['link']);
        if($urls['path']) {
            $tencentCDN->flush_paths($urls['path']);
        }
    } else {
        $tencentCDN->flush_urls([$post_url]);
    }

}
add_action('deleted_post', 'cdn_post_change', 10, 1); //删除文章时
add_action('trashed_post', 'cdn_post_change', 10, 1); //移除文章到回收站
add_action('untrashed_post', 'cdn_post_change', 10, 1); //从回收站恢复文章

//删除分类时刷新缓存
function cdn_delete_term($term, $tt_id, $taxonomy) {
    $tencentCDN = cdn_init_class_cdn('term');
    if($tencentCDN === false) {
        return;
    }

	$url = get_term_link($term, $taxonomy);
    $paths = [];
    if(get_option('permalink_structure')) {
        $paths[] = $url . '/page/';
    }

    $tencentCDN->flush_urls([$url]);
    if($paths) {
        $tencentCDN->flush_paths($paths);
    }
}
add_action('delete_term', 'cdn_delete_term', 10, 3);

//发表文章更新首页、分类页、标签页缓存
function cdn_insert_post($post_ID, $post, $update) {
	if($update || $post->post_status != 'publish') return;

    $tencentCDN = cdn_init_class_cdn('addpost');
    if($tencentCDN === false) {
        return;
    }

    $post_url = get_permalink($post_ID);

    if('post' == $post->post_type) {
        $home_url = home_url();
        $urls = cdn_get_term_urls($post_ID);
        array_push($urls['link'], $home_url, $post_url);

        if(get_option('permalink_structure')) {
            array_push($urls['path'], $home_url . '/page/');
        }

        $tencentCDN->flush_urls($urls['link']);
        if($urls['path']) {
            $tencentCDN->flush_paths($urls['path']);
        }
    } else {
        $tencentCDN->flush_urls([$post_url]);
    }
    
}
add_action('save_post_post', 'cdn_insert_post', 10, 3);

//刷新评论缓存
function cdn_comment($comment, $type='newcomment') {
    $tencentCDN = cdn_init_class_cdn($type);
    if($tencentCDN === false) {
        return;
    }

    if(is_int($comment)) {
        $comment = get_comment($comment);
    }

    $post_ID = $comment->comment_post_ID;
    $post_url = get_permalink($post_ID);
    $tencentCDN->flush_urls([$post_url]);
}

//发表评论时刷新缓存
function cdn_comment_post($comment_ID, $comment_approved) {
    if($comment_approved != 1) return;
    cdn_comment($comment_ID);
}
add_action('comment_post', 'cdn_comment_post', 10, 2);

//评论审核通过时刷新缓存
function cdn_comment_unapproved_to_approved($comment) {
    cdn_comment($comment);
}
add_action('comment_unapproved_to_approved', 'cdn_comment_unapproved_to_approved', 10, 1);

//更新评论时刷新缓存
function cdn_comment_change($comment_ID, $comment) {
    if(!($comment instanceof WP_Comment)) {
        $comment = $comment_ID;
    }
    cdn_comment($comment, 'editcomment');
}
add_action('edit_comment', 'cdn_comment_change', 10, 2); //编辑评论
add_action('deleted_comment', 'cdn_comment_change', 10, 2); //删除评论
add_action('trashed_comment', 'cdn_comment_change', 10, 2); //评论移至回收站
add_action('untrashed_comment', 'cdn_comment_change', 10, 2); //评论从回收站移出
add_action('spammed_comment', 'cdn_comment_change', 10, 2); //标记为垃圾评论
add_action('unspammed_comment', 'cdn_comment_change', 10, 2); //从垃圾评论恢复
?>